﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;
using UMC.Net;

namespace UMC.Wiki.Activities
{
    class SubjectDataActivity : WebActivity
    {

        List<WebMeta> Portfolio(ulong project_item_id, ulong subid)
        {

            var Portfolios = DataFactory.Instance().Portfolio(new ProjectItem { Id = project_item_id });

            var data = new List<WebMeta>();

            if (Portfolios.Length > 0)
            {
                var subs = DataFactory.Instance().Subject(Portfolios);

             
                foreach (var p in Portfolios)
                {
                    var meta = new WebMeta();
                    meta.Put("id", p.Id).Put("text", p.Caption);
                    var csubs = subs.Where(s => s.portfolio_id == p.Id);
                    var dcsub = new List<WebMeta>();
                    foreach (var cs in csubs)
                    {
                        var mta = new WebMeta().Put("id", cs.Id).Put("text", cs.Title);

                        dcsub.Add(mta); ;
                    }
                    meta.Put("subs", dcsub);
                    if (dcsub.Count == 0)
                    {
                        continue;
                    }
                    data.Add(meta);
                }
            }
            return data;

        }
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var strId = this.AsyncDialog("Id", g =>
            {
                return new Web.UITextDialog() { Title = "主题" };
            });
            Subject sub;
            var sid = UMC.Data.Utility.UInt64Parse(strId, 0);
            if (sid > 0)
            {
                sub = DataFactory.Instance().Subject(sid);
            }
            else
            {
                var codes = new List<String>(strId.Split('/'));
                switch (codes.Count)
                {
                    case 1:
                        codes.Insert(0, "Help");
                        codes.Insert(0, "UMC");
                        break;
                    case 2:
                        codes.Insert(0, "UMC");
                        break;
                    case 3:
                        break;
                    default:
                        response.Redirect(new WebMeta("Id", strId));
                        break;
                }


                var team = DataFactory.Instance().Project(codes[0]);
                if (team == null)
                {
                    response.Redirect(new WebMeta("Id", strId));
                }

                var projectItem = DataFactory.Instance().ProjectItem(team.Id.Value, codes[1]);

                if (projectItem == null)
                {
                    response.Redirect(new WebMeta("Id", strId));
                }
                sub = DataFactory.Instance().Subject(projectItem.Id.Value, codes[2]);
            }
            
            var user = this.Context.Token.Identity();


            var hash =Utility.AppendDictionary(sub);
            

            var con = DataFactory.Instance().Content(sub.Id.Value) ?? new SubjectContent();

            hash["ReleaseDate"] = UMC.Data.Utility.GetDate(sub.ReleaseTime);
            hash["Content"] = UMC.Data.JSON.Expression(String.IsNullOrEmpty(con.DataJSON) ? "[]" : con.DataJSON);
            hash.Remove("DataJSON");

            if (sub.project_item_id.HasValue)
            {
                hash["Portfolio"] = Portfolio(sub.project_item_id.Value, sub.Id.Value);
            }

            response.Redirect(hash);
        }

    }
}